/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package treeship.core.archetype.impl;

import java.util.ArrayList;
import java.util.List;

import treeship.core.algorithm.EICG;
import treeship.core.archetype.ArchetypeShip;

/**
 * 
 * @author Moonchild
 */
public class ArchetypeShipImpl extends ArchetypeShip {

	private List<PropertyContextImpl> properties = new ArrayList<PropertyContextImpl>();
	private List<ComponentContextImpl> components = new ArrayList<ComponentContextImpl>();
	private List<ArchetypeShipImpl> creatives = new ArrayList<ArchetypeShipImpl>();
	private String name;
	private DimensionContextImpl dimensionContext = new DimensionContextImpl();
	private long id;
	private boolean open = false;
	private boolean solid = false;
	private ArchetypeFamilyImpl family;

	public ArchetypeShipImpl() {
	}

	public ArchetypeShipImpl(String nom) {
		name = nom;
		id = nom.hashCode();
	}

	public void addProperty(PropertyContextImpl prop) {
		properties.add(prop);
	}

	public void addComponent(ArchetypeShipImpl component, Double wght) {
		components.add(new ComponentContextImpl(component, wght));
	}

	public void addCreative(ArchetypeShipImpl product) {
		creatives.add(product);
	}

	public void setName(String n) {
		name = n;
	}

	@Override
	public List<PropertyContextImpl> getProperties() {
		return properties;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public DimensionContextImpl getDimensionContext() {
		return dimensionContext;
	}

	@Override
	public List<ComponentContextImpl> getComponents() {
		return components;
	}

	@Override
	public List<ArchetypeShipImpl> getCreatives() {
		return creatives;
	}

	public String describe() {
		StringBuffer str = new StringBuffer();
		str.append("Ship: " + name + " of dimension "
				+ dimensionContext.getDimensionAverage() + ";"
				+ dimensionContext.getDimensionVariant() + ";"
				+ dimensionContext.getFreeSpaceAverage() + ";"
				+ dimensionContext.getFreeSpaceVariant());
		for (PropertyContextImpl prp : properties) {
			str.append("\n with property " + prp.getProperty().getName() + " "
					+ prp.getAverage() + " +- " + prp.getVariant() + " min "
					+ prp.getMin());
		}
		for (ComponentContextImpl shp : components) {
			str.append("\n made of " + shp.getComponent().getName() + " "
					+ shp.getWeight());
		}
		for (ArchetypeShip shp : creatives) {
			str.append("\n can create " + shp.getName());
		}

		return str.toString();
	}

	@Override
	public String toString() {
		return name;
	}

	public void normalizeWeights() {
		double sommapesi = 0.0;
		for (ComponentContextImpl cc : components) {
			sommapesi += cc.getWeight();
		}

		for (ComponentContextImpl cc : components) {
			Double vecchiopeso = cc.getWeight();
			cc.setWeight(vecchiopeso / sommapesi);
		}
	}

	public void setId(long id) {
		this.id = id;
	}

	@Override
	public long getId() {
		return id;
	}

	public void dangerous_removeComponent(ComponentContextImpl comp) {
		components.remove(comp);
	}

	public void dangerous_removeProperty(PropertyContextImpl prop) {
		properties.remove(prop);
	}

	@Override
	public Double getStaticPropertyForString(String s) {
		double valore = EICG.staticGetRandDouble(this.getId() + s.hashCode());
		return valore;
	}

	@Override
	public boolean isOpen() {
		return open;
	}

	public void setOpen(boolean open) {
		this.open = open;
	}

	@Override
	public boolean isSolid() {
		return solid;
	}

	public void setSolid(boolean solid) {
		this.solid = solid;
	}

	@Override
	public ArchetypeFamilyImpl getFamily() {
		return family;
	}

	public void setFamily(ArchetypeFamilyImpl family) {
		this.family = family;
	}
}
